package com.audi.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.audi.R;
import com.audi.dto.HistoryActionDateTime;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;

public class HistoryAdapter extends BaseExpandableListAdapter {

    private Context mcontext;
    private ArrayList<String> dateHistory;
    private HashMap<String, ArrayList<HistoryActionDateTime>> listHistoryAction;
    private HistoryActionDateTime historyAction ;
    private ExpandableListView exListView;
    public HistoryAdapter(Context mcontext, ArrayList<String> dateHistory,
	    HashMap<String, ArrayList<HistoryActionDateTime>> listHistoryAction) {
	this.mcontext = mcontext;
	this.dateHistory = dateHistory;
	this.listHistoryAction = listHistoryAction;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
	return this.listHistoryAction.get(this.dateHistory.get(groupPosition))
		.get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
	// TODO Auto-generated method stub
	return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
	    boolean isLastChild, View convertView, ViewGroup parent) {
	if (convertView == null) {
	    LayoutInflater inflater = (LayoutInflater) this.mcontext
		    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
	    convertView = inflater.inflate(R.layout.list_item_history_date,
		    null);
	}
	ImageView iconHistory = (ImageView) convertView
		.findViewById(R.id.icon_history);
	TextView statusDevice = (TextView) convertView
		.findViewById(R.id.status_device);
	TextView deviceAtPlace = (TextView) convertView
		.findViewById(R.id.device_at_place);
	TextView timeDate = (TextView) convertView
		.findViewById(R.id.hour_date_history);
	 historyAction = (HistoryActionDateTime) getChild(
		groupPosition, childPosition);

	if (historyAction != null) {
	    iconHistory.setImageResource(R.drawable.icon_history);
	    statusDevice.setText(historyAction.getStatusHistory());
	    deviceAtPlace.setText(historyAction.getDevicePlace());
	    timeDate.setText(historyAction.getTimeHistory());

	}
	return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
	return this.listHistoryAction.get(this.dateHistory.get(groupPosition))
		.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
	return this.dateHistory.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
	return dateHistory.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
	return groupPosition;
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
	// TODO Auto-generated method stub
	super.onGroupExpanded(groupPosition);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
	    View convertView2, ViewGroup parent) {
	String date = (String) getGroup(groupPosition);
	if (convertView2 == null) {
	    LayoutInflater inflater = (LayoutInflater) this.mcontext
		    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    convertView2 = inflater.inflate(R.layout.row_parent_history, null);
	}
	TextView dateHistory = (TextView) convertView2
		.findViewById(R.id.tv_parent_date);
	dateHistory.setText(date);

	return convertView2;
    }

    @Override
    public boolean hasStableIds() {
	return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
	return false;
    }

}
